package com.ruoyi.cms.controller.admin;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.cms.common.BaseController;
import com.ruoyi.cms.domain.Field;
import com.ruoyi.cms.domain.Form;
import com.ruoyi.cms.exception.AdminGeneralException;
import com.ruoyi.cms.exception.CmsException;
import com.ruoyi.cms.exception.TransactionException;
import com.ruoyi.cms.service.FieldService;
import com.ruoyi.cms.service.FormService;
import com.ruoyi.cms.util.ExceptionEnum;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cms/field")
public class FieldController extends BaseController {
    @Autowired
    private FormService formService;
    @Autowired
    private FieldService fieldService;

    @GetMapping("/list")
    public R<List<Field>> list(@RequestParam("formId") String formId) {
        return R.ok(fieldService.queryFieldByFormId(formId));
    }

    @GetMapping("/find/{id}")
    public R<Field> getField(@PathVariable(name = "id") String id) {
        return R.ok(fieldService.queryFieldById(id));
    }

    @PostMapping("/save")
    public R<String> saveField(@RequestBody Field field) throws CmsException {
        field.setId(IdUtil.getSnowflake().nextIdStr());
        field.setCreateBy(LoginHelper.getLoginUser().getLoginId());
        field.setCreateTime(new Date());
        if(StringUtils.isBlank(""+field.getMaxLength())) {
            field.setMaxLength(200);
        }
        try {
            fieldService.add(field);
        } catch (TransactionException e) {
            throw new AdminGeneralException(
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
                e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/update")
    public R<String> updateField(@RequestBody Field newField) throws CmsException {
        Form form = formService.queryFormById(newField.getFormId());

        Field oldField = fieldService.queryFieldById(newField.getId());
        newField.setUpdateBy(LoginHelper.getLoginUser().getLoginId());
        newField.setUpdateTime(new Date());
        if(StringUtils.isBlank(""+newField.getMaxLength())) {
            newField.setMaxLength(200);
        }
        try {
            fieldService.update(newField,form,oldField);
        } catch (TransactionException e) {
            throw new AdminGeneralException(
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
                e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("delete/{id}")
    public int deleteField(@PathVariable(value = "id", required = false)String id) throws CmsException {
        Field field = fieldService.queryFieldById(id);
        Form form = formService.queryFormById(field.getFormId());
        if(form.getType() == 0) {
            throw new RuntimeException("系统表单不允许删除！");
        }
        try {
            return fieldService.delete(form,field);
        } catch (TransactionException e) {
            throw new AdminGeneralException(
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
                e.getMessage());
        }
    }
}
